package com.zjlp.seproject.service.impl;

import com.zjlp.seproject.mapper.*;
import com.zjlp.seproject.pojo.*;
import com.zjlp.seproject.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    private SubjectMapper subjectMapper;
    @Autowired
    private AnnexMapper annexMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ProfileMapper profileMapper;

    public List<Subject> selectAll(){
        return subjectMapper.selectAll();
    }

    public int deleteByPrimaryKey(Integer id){
        return subjectMapper.deleteByPrimaryKey(id);
    }

    public int insert(Subject record){
        return subjectMapper.insert(record);
    }

    public SubjectDetails selectByPrimaryKey(Integer id){
        Subject s =  subjectMapper.selectByPrimaryKey(id);
        SubjectDetails sd = new SubjectDetails(s);
        User u = userMapper.getUsername(s.getUid());
        sd.setUsername(u.getUsername());
        sd.setAvatar(u.getAvatar());
        sd.setProfile(profileMapper.profile(s.getUid()));
        sd.setClassname(courseMapper.getName(s.getClassid()));
        return sd;
    }

    public int updateByPrimaryKey(Subject record){
        return subjectMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<BrifSubject> selectBrif(Integer id) {
        if (id == null) {
            return null;
        }
        List<BrifSubject> all = subjectMapper.selectBrif();
        List<BrifSubject> result = new ArrayList<>();
        int num;
        switch (id) {
            case 0:
                num = 7;    //其他
                break;
            case 1:        //数据结构
                num = 5;
                break;
            case 2:        //操作系统
                num = 4;
                break;
            case 3:        //数据库
                num = 2;
                break;
            case 4:        //软件工程
                num = 1;
                break;
            case 5:        //计算机网络
                num = 3;
                break;
            case 6:        //高级程序设计
                num = 6;
                break;
            case 7:
                num = 7;
                break;
            default:
                return null;
        }
        for (BrifSubject subject : all) {
            if (subject.getClassid() == num) {
                result.add(subject);
            }
        }
        return result;
    }

    @Override
    public Integer addSubject(AddSubject addSubject) {
        Subject subject = new Subject();
        Integer id = courseMapper.getid(addSubject.getClassid());
        if (id == null) {
            return 0;
        }
        subject.setClassid(id);
        subject.setName(addSubject.getName());
        subject.setDescription(addSubject.getDescription());
        subject.setUrl(addSubject.getUrl());
        subject.setUid(addSubject.getUid());
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        subject.setDate(simpleDateFormat.format(date));
//        System.out.println(simpleDateFormat.format(date));
        subject.setStar(0);
        subject.setClick(0);
        subjectMapper.addSubject(subject);
        List<String> urls = addSubject.getList();
        for (String s : urls) {
            annexMapper.setAnnex(subject.getId(), s);
        }
        return 1;
    }

    @Override
    public int click(Integer id, Integer click) {
        return subjectMapper.click(id, click + 1);
    }

    @Override
    public List<SubjectDetails> getNewSubject() {
        List<Subject> list = subjectMapper.getNewSubject();
        List<SubjectDetails> result = new ArrayList<>();
        for (Subject s : list) {
            SubjectDetails sd = new SubjectDetails(s);
            User u = userMapper.getUsername(s.getUid());
            sd.setUsername(u.getUsername());
            sd.setAvatar(u.getAvatar());
            sd.setProfile(profileMapper.profile(s.getUid()));
            sd.setClassname(courseMapper.getName(s.getClassid()));
            result.add(sd);
        }
        return result;
    }

    @Override
    public Integer allowSubject(Integer id) {
        return subjectMapper.allowSubject(id);
    }

    @Override
    public Integer setAnnex(Annex annex) {
        List<String> urls = annex.getList();
        for (String s : urls) {
            annexMapper.setAnnex(annex.getId(), s);
        }
        return null;
    }

    @Override
    public List<String> getAnnex(Integer sid) {
        return annexMapper.getAnnex(sid);
    }

    @Override
    public SubjectDetails adminSubject(Integer id) {
        Subject s = subjectMapper.adminSubject(id);
        SubjectDetails sd = new SubjectDetails(s);
        User u = userMapper.getUsername(s.getUid());
        sd.setUsername(u.getUsername());
        sd.setAvatar(u.getAvatar());
        sd.setProfile(profileMapper.profile(s.getUid()));
        sd.setClassname(courseMapper.getName(s.getClassid()));
        return sd;
    }

    @Override
    public Integer refuseSubject(Integer id) {
        return subjectMapper.refuseSubject(id);
    }

    @Override
    public Integer deleteSubject(Integer id) {
        return subjectMapper.deleteSubject(id);
    }

    @Override
    public List<Subject> personalSubject(Integer id) {
        return subjectMapper.personalSubject(id);
    }
}
